package Q385;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static void main(String[] args) {

    }
}

class NestedInteger {
    public NestedInteger(int value) {

    }

    public NestedInteger() {

    }

    public void add(NestedInteger nestedInteger){

    }
}

class Solution {
    public NestedInteger deserialize(String s) {
        if (s.charAt(0) != '[') {
            return new NestedInteger(Integer.parseInt(s));
        }
        Deque<NestedInteger> queue = new ArrayDeque<>();
        int num = 0;
        boolean flag = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '-') {
                flag = true;
            } else if (Character.isDigit(c)) {
                num = num * 10 + c - '0';
            }else if(c == '['){
                queue.push(new NestedInteger());
            }else if (c == ',' || c == ']'){
                if(Character.isDigit(s.charAt(i - 1))){
                    if(flag){
                        num *= -1;
                    }
                    queue.peek().add(new NestedInteger(num));
                }
                num = 0;
                flag = false;

                if(c == ']' && queue.size() > 1){
                    NestedInteger pop = queue.pop();
                    queue.peek().add(pop);
                }
            }
        }
        return queue.pop();
    }
}
